package cn.itsource.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.Logs;
import cn.itsource.domain.Employee;
import cn.itsource.domain.Employee;
import cn.itsource.domain.Logininfo;
import cn.itsource.domain.Role;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.mapper.EmployeeMapper;
import cn.itsource.mapper.LogininfoMapper;
import cn.itsource.query.EmployeeQuery;
import cn.itsource.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper LogininfoMapper;
    @Logs
    @Transactional
    @Override
    public void add(Employee employee) {
        //密码加密
        String salt = RandomUtil.randomString(32);
        String md5password = SecureUtil.md5(employee.getPassword()+ salt);
        employee.setSalt(salt);
        employee.setPassword(md5password);

        //同步信息到logiininfo
        //根据属性的同名原则进行属性拷贝
        Logininfo logininfo = new Logininfo();
        //源（employee） 拷贝到 目标（logininfo）
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        //判断是否为禁用
        logininfo.setDisable(employee.getState() == 0?false:true);
        LogininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId()); //生成自增ID
        employeeMapper.add(employee);
        if (employee.getMyRoles().size()>0){
            employeeMapper.addEmpRoles(employee);
        }
    }
    @Logs
    @Transactional
    @Override
    public void update(Employee employee) {

        employeeMapper.update(employee);
        employeeMapper.deRole(employee.getId());
        if (employee.getMyRoles().size()>0){
            employeeMapper.addEmpRoles(employee);
        }
        //修改logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setId(employee.getLogininfoId());
        logininfo.setType(0);
        logininfo.setDisable(employee.getState() == 0?false:true);
        LogininfoMapper.update(logininfo);

    }
    @Logs
    @Transactional
    @Override
    public void del(Long id) {
        //删除logininfo信息
        Employee employee = employeeMapper.get(id);
        LogininfoMapper.del(employee.getId());

        //删除员工
        employeeMapper.del(id);
        //删除员工角色ID
        employeeMapper.deRole(id);
    }
    @Logs
    @Transactional
    @Override
    public void delList(List<Long> ids) {
        employeeMapper.delList(ids);
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.get(id);
    }


    @Override
    public PageInfo<Employee> pageQuery(EmployeeQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Employee> employees = employeeMapper.pageQuery(query);
        for (Employee employee : employees){
            List<Long> ids = employeeMapper.findRoleByEmpId(employee.getId());
            employee.setMyRoles(ids);

            List<String> roleNames = employeeMapper.findRoleNameByEmpId(employee.getId());
            employee.setRoles(roleNames);
        }

        return new PageInfo<>(employees);
    }
}
